//
//  DcAFNetWorkTool.m
//  艾薇贝儿
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 dllo－－邴鹏超. All rights reserved.
//

#import "DcAFNetWorkTool.h"
#import "AFNetworking.h"
@implementation DcAFNetWorkTool
/**
 *  Description
 *
 *  @param url         网络请求地址
 *  @param body        请求体
 *  @param result      返回数据类型
 *  @param headerFlile 请求头
 *  @param success     网络请求成功回调
 *  @param failure     网络请求失败回调
 */
+(void)getUrl:(NSString * )url
         body:(id)body
       result:(DcResult)result
  headerFlile:(NSDictionary *)headerFlile
      success:(void (^)(id result))success
      failure:(void (^)(NSError *error))failure
{
    
    //1.获取网络请求管理类
    AFHTTPRequestOperationManager * manager=[AFHTTPRequestOperationManager manager];
    
//    //2.设置网络请求返回值所支持的类型
//    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    
    //3.为网络请求加请求头
    if (headerFlile) {
        for (NSString * key in headerFlile.allKeys) {
            [manager.requestSerializer setValue:headerFlile[key] forHTTPHeaderField:key];
        }
    }
 
    //4.设置返回值类型
    
    switch (result) {
        case DcData:
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            break;
            case DcJSON:
            manager.responseSerializer=[AFJSONResponseSerializer serializer];
            break;
            case DCXML:
            manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //2.设置网络请求返回值所支持的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    
//    manager.responseSerializer=[AFJSONResponseSerializer serializer];
//    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
//    manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
    
    //5.发送网络请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            //成功回调
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error) {
            //失败回调
            failure(error);
        }
    }];
}
/**
 *  POST请求
 *
 *  @param url          网络请求地址
 *  @param body         请求体
 *  @param result       返回值类型
 *  @param requestStyle 网络请求body类型
 *  @param headerFlile  网络请求头
 *  @param success      成功回调
 *  @param failure      失败回调
 */

+(void)postUrl:(NSString * )url
          body:(id)body
        result:(DcResult)result
  requestStyle:(DcResultStyle)requestStyle
   headerFlile:(NSDictionary *)headerFlile
       success:(void (^)(id result))success
       failure:(void (^)(NSError *error))failure
{
    //1.获取网络请求管理类
    AFHTTPRequestOperationManager * manager=[AFHTTPRequestOperationManager manager];
//    
//    //2.设置网络返回值支持的类型
//    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    
    // 3.网络请求请求体body的类型
    switch (requestStyle) {
        case DcResultJSON:
            manager.requestSerializer=[AFJSONRequestSerializer serializer];
            break;
        case DcResultString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    // 4.为网络请求加请求头
    if (headerFlile) {
        for (NSString *key in headerFlile.allKeys) {
            [manager.requestSerializer setValue:headerFlile[key] forHTTPHeaderField:key];
        }
    }
    // 5.网络请求返回值的数据类型
    switch (result) {
        case DcData:
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            break;
        case DcJSON:
            break;
        case DCXML:
            manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //2.设置网络返回值支持的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    // 6.发送网络请求
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            // 成功回调
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error) {
            // 失败回调
            failure(error);
        }
        
    }];
}
@end
